package eu.telecombretagne.rsm.gui.frames.objects;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

public class AutomateLegend {
	private Integer x = 100;
	private Integer y = 50;
	
	private Color colorLegendText;
	private Color colorLegendFirst;
	private Color colorLegendSecond;
	
	private String titleFirst;
	private String titleSecond;
	
	private Float offsetX;
	
	private Rectangle2D.Float firstRectangle;
	private Rectangle2D.Float secondRectangle;
	
	/**
	 * The constructor of the AutomateLegend.
	 * 
	 * @param pX An X coordinate of the position.
	 * @param pY An Y coordinate of the position.
	 * @param pTitleFirst A first title string.
	 * @param pTitleSecond A second title string.
	 * @param pColorLegendText A text color.
	 * @param pColorFirst A color of the first field.
	 * @param pColorSecond A color of the second field.
	 */
	public AutomateLegend(Integer pX, Integer pY, String pTitleFirst, String pTitleSecond, Color pColorLegendText, Color pColorFirst, Color pColorSecond){
		x = pX;
		y = pY;
		
		titleFirst = pTitleFirst;
		titleSecond = pTitleSecond;
		
		colorLegendText = pColorLegendText;
		colorLegendFirst = pColorFirst;
		colorLegendSecond = pColorSecond;
	}
	
	/**
	 * Performs a drawing action.
	 * @param g A Graphics object.
	 */
	public void draw(Graphics g){
		Graphics2D g2 = (Graphics2D) g;
		
		firstRectangle = new Rectangle2D.Float(x, y, 25, 15);
		g2.setColor(colorLegendFirst);
		g2.fill(firstRectangle);
		
		drawTitleForRectangle(g2, firstRectangle, titleFirst, colorLegendText);
		
		secondRectangle = new Rectangle2D.Float(offsetX, y, 25, 15);
		g2.setColor(colorLegendSecond);
		g2.fill(secondRectangle);
		
		drawTitleForRectangle(g2, secondRectangle, titleSecond, colorLegendText);
	}
	
	private void drawTitleForRectangle(Graphics2D g, Rectangle2D.Float pRectangle, String pTitle, Color pColor){
		FontMetrics fontMetrics  = g.getFontMetrics();
		Rectangle2D stringBounds = fontMetrics.getStringBounds(pTitle, g);
		Float centerFY = (float) stringBounds.getCenterY();
		
		Float positionX = pRectangle.x + pRectangle.width + 5;
		Float positionY = (float) (pRectangle.getCenterY() - centerFY);
		
		g.setColor(pColor);
		g.drawString(pTitle, positionX, positionY);
		
		offsetX = (float) stringBounds.getWidth() + positionX + 10;
	}
}